﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RadarMonitor.ViewModel;

namespace RadarMonitor
{
    /// <summary>
    /// Interaction logic for ShowRecordsWindows.xaml
    /// </summary>
    public partial class ShowRecordsWindows : Window
    {
        private LogCardViewModel logCardViewModel;
        public ShowRecordsWindows()
        {
            InitializeComponent();
            
        }

        private void priorLogPage(object sender, RoutedEventArgs e)
        {
            var num = logCardViewModel.PageNum - 1;
            logCardViewModel.gotoPage(num);
        }

        private void nextLogPage(object sender, RoutedEventArgs e)
        {
            var num = logCardViewModel.PageNum + 1;
            logCardViewModel.gotoPage(num);
        }

        private void PageNumTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    var num = int.Parse(PageNumTB.Text);
                    logCardViewModel.gotoPage(num);
                    PageNumTB.Text = logCardViewModel.PageNum.ToString();
                }
                catch { }
            }
        }

        private void PageNumTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                var num = int.Parse(PageNumTB.Text);
                logCardViewModel.gotoPage(num);
                PageNumTB.Text = logCardViewModel.PageNum.ToString();
            }
            catch { }
        }

        private void AlarmTimeComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (logCardViewModel == null)
            {
                return;
            }
            int index = AlarmTimeComBox.SelectedIndex;
            if (index != logCardViewModel.dateFilter)
            {
                logCardViewModel.dateFilter = index;
                logCardViewModel.update();
            }
        }

        private void AlarmSourceComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (logCardViewModel == null)
            {
                return;
            }
            int index = AlarmSourceComBox.SelectedIndex;
            if (index != logCardViewModel.sourceFilter)
            {
                logCardViewModel.sourceFilter = index;
                logCardViewModel.update();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            bool update = logCardViewModel.sourceFilter !=0 ||
                          logCardViewModel.dateFilter != 0;
           
            logCardViewModel.sourceFilter = 0;
            logCardViewModel.dateFilter = 0;

            if (update)
            {
                logCardViewModel.update();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            logCardViewModel = DataContext as LogCardViewModel;
            AlarmSourceComBox.SelectedIndex = logCardViewModel.sourceFilter;
            bool update = logCardViewModel.sourceFilter != 0;
            logCardViewModel.dateFilter = 0;

            if (update)
            {

                logCardViewModel.update();
            }
        }
    }
}
